<template>
  <NModal :show="state.show" @UpdateShow="close">
    <div ref="modalRef" class="list-dia">
      <MacHeader
        :title="state.title"
        :isFullscreen="isFullscreen"
        :showDesc="false"
        @toggle="toggle"
        @close="close"
      />
      <div class="content">
        <div>
          <NForm
            inline
            :model="tableInfo.queryForm"
            label-width="auto"
            label-placement="left"
          >
            <NFormItem label="类型">
              <n-select
                style="width: 150px"
                clearable
                v-model:value="tableInfo.queryForm.type"
                :options="typeEnum"
              />
            </NFormItem>
            <NFormItem label="关联资源">
              <NInput
                v-model:value="tableInfo.queryForm.about_id"
                placeholder="请输入关联id"
                clearable
                style="width: 200px"
              />
            </NFormItem>
            <NFormItem>
              <NButton type="primary" @click="queryData">
                <template #icon>
                  <n-icon>
                    <i class="esayIcon esayIcon-sousuo"></i>
                  </n-icon>
                </template>
                <span>搜索</span>
              </NButton>
            </NFormItem>
          </NForm>
        </div>
        <NDataTable
          style="flex: 1"
          flex-height
          :loading="tableInfo.listLoading"
          :columns="columns"
          :data="tableInfo.tableData"
          :pagination="false"
        />
        <div class="pagination">
          <NPagination
            v-model:page="tableInfo.queryForm.pageNo"
            :page-count="tableInfo.total"
            :page-sizes="tableInfo.pageSizes"
            show-quick-jumper
            show-size-picker
            @updatePage="pageChange"
            @updatePageSize="sizeChange"
          />
        </div>
      </div>
    </div>
  </NModal>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { Ref } from 'vue'
import {
  DataTableColumns,
  NModal,
  NDataTable,
  NPagination,
  NFormItem,
  NButton,
  NForm,
  NIcon,
  NSelect,
  NInput
} from 'naive-ui'
import type { SelectOption } from 'naive-ui'
import MacHeader from '@/components/codeBlock/MacHeader'
import { useFullscreen } from '@vueuse/core'
import { useApi } from '@/hooks/useApi'
import userApi from '@/service/api/operation/user'
import typeEnum, { getInterTypeName } from '@/enum/InterTypeEnum'

const addRef: Ref<any> = ref()
const delRef: Ref<any> = ref()
const { tableInfo, pageChange, sizeChange, queryData } = useApi({
  isRequest: false,
  addRef,
  delRef,
  getData: userApi.logList,
  dataMap: (data: any) => {
    return data.map((el: any) => {
      el.type = getInterTypeName(el.type)
      return el
    })
  },
  initForm: {
    userId: ''
  }
})

const columns: DataTableColumns<any> = [
  {
    title: '关联id',
    key: 'about_id',
    width: 80
  },
  {
    title: '类型',
    key: 'type'
  },
  {
    title: '积分',
    key: 'integral'
  },
  {
    title: '总积分',
    key: 'allIntegral'
  },
  {
    title: '说明',
    key: 'content',
    width: 200,
    ellipsis: {
      tooltip: true
    }
  },
  {
    title: '日期',
    key: 'create_at'
  }
]

const state = reactive({
  show: false,
  title: ''
})
const modalRef: Ref<any> = ref()
const {
  isFullscreen,
  toggle
}: { isFullscreen: Ref<boolean>; toggle: () => Promise<void> } =
  useFullscreen(modalRef)
const close = () => {
  state.show = false
}
const open = (row: any): void => {
  state.title = `${row.nickname}[积分记录]`
  tableInfo.queryForm.userId = row.id
  queryData()
  state.show = true
}

defineExpose({
  open
})
</script>
<style scoped lang="scss">
.list-dia {
  width: 1000px;
  height: 80vh;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  .content {
    margin-top: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
  .add-text {
    margin-left: 10px;
  }
  .pb20 {
    padding-bottom: 20px;
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
